package org.tlipoca.smarticlefavorites.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.genntii.smcommon.constant.ExceptionConstant;
import org.genntii.smcommon.context.BaseContext;
import org.genntii.smcommon.exception.ArticleFavoritesNotFoundException;
import org.genntii.smcommon.exception.ArticleNotFoundException;
import org.genntii.smcommon.exception.UserDirectoryNotFoundException;
import org.genntii.smcommon.exception.UserNotFoundException;
import org.genntii.smdomain.dto.ArticleFavoritesDTO;
import org.genntii.smdomain.entity.ArticleFavorites;
import org.genntii.smdomain.vo.ArticleFavoritesCountVO;
import org.genntii.smdomain.vo.ArticleVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.tlipoca.smarticlefavorites.client.ArticleClient;
import org.tlipoca.smarticlefavorites.client.UserClient;
import org.tlipoca.smarticlefavorites.client.UserDirectoryClient;
import org.tlipoca.smarticlefavorites.mapper.ArticleFavoritesMapper;
import org.tlipoca.smarticlefavorites.service.ArticleFavoritesService;

import java.util.ArrayList;

@Slf4j
@Service
public class ArticleFavoritesServiceImpl extends ServiceImpl<ArticleFavoritesMapper, ArticleFavorites> implements ArticleFavoritesService {

    @Autowired
    private ArticleFavoritesMapper articleFavoritesMapper;

    @Autowired
    private UserDirectoryClient userDirectoryClient;

    @Autowired
    private ArticleClient articleClient;

    @Autowired
    private UserClient userClient;

    @Override
    public ArrayList<ArticleVO> getArticleFavoritesByDirectoryIdService(Long directoryId) {
        // 判断收藏夹是否存在
        if (!userDirectoryClient.getUserDirectoryExists(directoryId).getData()) {
            throw new UserDirectoryNotFoundException(ExceptionConstant.USER_DIRECTORY_NOT_FOUND_EXCEPTION);
        }

        // 查询文章收藏
        ArrayList<ArticleFavorites> articleFavorites = articleFavoritesMapper.selectArticleFavoritesListByDirectoryId(directoryId);

        ArrayList<Long> articleIds = new ArrayList<>();
        for (ArticleFavorites articleFavorite : articleFavorites) {
            articleIds.add(articleFavorite.getArticleId());
        }

        // 根据收藏ID 查询文章
        ArrayList<ArticleVO> articleList = articleClient.getArticleListByIds(articleIds).getData();
        for (ArticleVO articleVO : articleList) {
            articleVO.setDirectoryId(directoryId);
            articleVO.setDirectoryUserId(articleFavorites.get(0).getUserId());
        }

        return articleList;
    }

    @Override
    public ArrayList<ArticleFavoritesCountVO> getArticleFavoritesCountByUserIdGroupByDirectoryIdService(Long userId) {
        if (!userClient.getUserExists(userId).getData()) {
            throw new UserNotFoundException(ExceptionConstant.USER_NOT_FOUND_EXCEPTION);
        }

        return articleFavoritesMapper.selectArticleFavoritesCountByUserId(userId);
    }

    @Override
    public void addArticleFavoritesService(ArticleFavoritesDTO articleFavoritesDTO, Long userId) {
        // 文章是否存在
        if (!articleClient.getArticleExists(articleFavoritesDTO.getArticleId()).getData()) {
            throw new ArticleNotFoundException(ExceptionConstant.ARTICLE_NOT_FOUND_EXCEPTION);
        }

        // 收藏夹是否存在
        if (!userDirectoryClient.getUserDirectoryExists(articleFavoritesDTO.getDirectoryId()).getData()) {
            throw new UserDirectoryNotFoundException(ExceptionConstant.USER_DIRECTORY_NOT_FOUND_EXCEPTION);
        }

        // 插入Mysql
        articleFavoritesMapper.insert(ArticleFavorites.builder()
                .articleId(articleFavoritesDTO.getArticleId())
                .userId(userId)
                .directoryId(articleFavoritesDTO.getDirectoryId())
                .build());
    }

    @Override
    public void deleteArticleFavoritesService(Long favoritesId) {
        // 判断收藏是否存在
        if(!articleFavoritesMapper.exists(new QueryWrapper<ArticleFavorites>().eq("id", favoritesId))) {
            throw new ArticleFavoritesNotFoundException(ExceptionConstant.ARTICLE_NOT_FAVORITES_EXCEPTION);
        }

        articleFavoritesMapper.deleteById(favoritesId);
    }

    @Override
    public void deleteArticleFavoritesBatchService(ArrayList<Long> favoritesIds) {
        // 判断收藏是否存在
        if(articleFavoritesMapper.existsBatch(favoritesIds) != favoritesIds.size()) {
            throw new ArticleFavoritesNotFoundException(ExceptionConstant.ARTICLE_NOT_FAVORITES_EXCEPTION);
        }

        articleFavoritesMapper.deleteBatch(favoritesIds);
    }
}
